//
//  HDBottomBarView.m
//  HDWuDaDaoServiceCenter
//
//  Created by HDNiuKuiming on 2017/4/8.
//  Copyright © 2017年 HDNiuKuiming. All rights reserved.
//

#import "HDBottomBarView.h"

@interface HDBottomBarView()
{
    NSInteger nowSelectIndex;
}
@property (weak, nonatomic) IBOutlet UIButton *mineBtn;
@property (weak, nonatomic) IBOutlet UIButton *newsBtn;
@property (weak, nonatomic) IBOutlet UIButton *audioBtn;
@property (weak, nonatomic) IBOutlet UIButton *searchBtn;

@property (weak, nonatomic) IBOutlet UILabel *mineLabel;
@property (weak, nonatomic) IBOutlet UILabel *newsLabel;
@property (weak, nonatomic) IBOutlet UILabel *audioLabel;
@property (weak, nonatomic) IBOutlet UILabel *searchLabel;

@end
@implementation HDBottomBarView

- (void)awakeFromNib
{
    [super awakeFromNib];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(rootVCScanSuccess) name:kRoot_Scan_Success_Notification object:nil];
}

- (void)hightedBtn:(NSInteger)btnIndex
{
    nowSelectIndex = btnIndex;
    NSArray *btnArray = @[_mineBtn,_newsBtn,_audioBtn,_searchBtn];
    for (NSInteger i=0;i<btnArray.count;i++) {
        UIButton *btnTemp = btnArray[i];
        if (i == btnIndex) {
            [btnTemp setHighlighted:YES];
        }
        else {
            [btnTemp setHighlighted:NO];
        }
    }
}

- (void)rootVCScanSuccess
{
    [self btnSelectAction:_audioBtn];
    [self hightedBtn:2];
}

#define Tag_Prefix 901

- (IBAction)btnSelectAction:(UIButton *)sender {
    NSInteger btnTag = sender.tag-Tag_Prefix;
    if (btnTag == 4) // 跳转到扫码页面
    {
        if ([_delegate respondsToSelector:@selector(barSelectAction:)]) {
            [_delegate barSelectAction:@"HDRootScanVC"];
        }
        return;
    }
    
    if (btnTag != nowSelectIndex) {
        NSArray *vcIdentifierArray = @[@"HDRootVC",
                                       @"HDRootNewsVC",
                                       @"HDRootAudioVC",
                                       @"HDRootSearchVC"];
        NSString *VCStr = nil;
        if (btnTag>=0 && btnTag < vcIdentifierArray.count)
            VCStr = vcIdentifierArray[btnTag];
        
        if (VCStr != nil && [_delegate respondsToSelector:@selector(barSelectAction:)]) {
            [_delegate barSelectAction:VCStr];
        }
    }

    else {
        NSArray *vcIdentifierArray = @[@"bar_mine_P",
                                       @"bar_news_P",
                                       @"bar_audio_P",
                                       @"bar_search_P"];
        if (btnTag < vcIdentifierArray.count) {
            UIImage *btnImg = [UIImage imageNamed:vcIdentifierArray[btnTag]];
            [sender setImage:btnImg forState:UIControlStateNormal];
        }
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
